package com.brilliance.project.common.tools;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PipedOutputStream;

/**
 * 处理stream的工具
 * 
 * @author HLJ
 * 
 */
public class StreamUtil {
	/**
	 * inputStream转outputStream
	 * 
	 * @param inputStream
	 * @return
	 */
	public static OutputStream input2OutputStream(InputStream inputStream) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		try {
			int k = 0;
			byte[] bytesIn = new byte[1024];
			while ((k = inputStream.read(bytesIn)) > -1) {
				baos.write(bytesIn, 0, k);
			}
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		return baos;
	}

	/**
	 * outputStream转inputStream
	 * 
	 * @param outputStream
	 * @return
	 */
	public static InputStream output2InputStream(OutputStream outputStream) {
		ByteArrayOutputStream baos = (ByteArrayOutputStream) outputStream;
		return new ByteArrayInputStream(baos.toByteArray());
	}
}
